﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Management;
using System.IO;
using System.Xml.Linq;
using ajayumi.develop.win32.Transport.Utils;
using ajayumi.develop.win32.Transport.WCF.Services.Interfaces;

namespace ajayumi.develop.win32.Transport.WCF.ServiceImpls
{
    /// <summary>
    /// 服务器硬盘实现
    /// Author  : 黄乙冬
    /// Date    : 2011-08-30
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class DriveService : IDrive
    {
        #region IServerDirectory 成员

        /// <summary>
        /// 列出下级目录
        /// </summary>
        /// <param name="parentDir">上级目录，如果上级目录为空，则列出根目录</param>
        /// <param name="hasFile">包含文件</param>
        /// <returns>上级目录下的子目录</returns>
        public string[] GetDirectories(string parentDir, bool hasFile)
        {
            List<string> lst = new List<string>();
            if (string.IsNullOrEmpty(parentDir))
            {
                SelectQuery sq = new SelectQuery("SELECT * FROM WIN32_LOGICALDISK");
                ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
                foreach (ManagementObject item in mos.Get())
                {
                    DriveType driveType = (DriveType)((uint)item["DriveType"]);
                    string driveName = string.Concat(item["Name"].ToString(), "\\");

                    switch (driveType)
                    {
                        case DriveType.CDRom:
                            break;
                        case DriveType.Fixed:
                            lst.Add(driveName);
                            break;
                        case DriveType.Network:
                            break;
                        case DriveType.NoRootDirectory:
                            break;
                        case DriveType.Ram:
                            break;
                        case DriveType.Removable:
                            break;
                        case DriveType.Unknown:
                            break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                try
                {
                    if (Directory.Exists(parentDir))
                    {
                        string[] directories = Directory.GetDirectories(parentDir);
                        foreach (string item in directories)
                        {
                            lst.Add(item);
                        }

                        if (hasFile)
                        {
                            string[] files = Directory.GetFiles(parentDir);
                            foreach (string item in files)
                            {
                                lst.Add(item);
                            }
                        }
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    return new string[] { };
                }
                catch (Exception ex)
                {
                    return new string[] { };
                }
            }

            return lst.ToArray();
        }

        /// <summary>
        /// 创建一个新的目录
        /// </summary>
        /// <param name="parentDir">上级目录</param>
        /// <param name="dirName">新目录名称</param>
        /// <returns>创建后的新目录路径</returns>
        public string CreateDir(string parentDir, string dirName)
        {
            string path = Path.Combine(parentDir, dirName);
            try
            {
                if (Directory.Exists(path))
                { return string.Empty; }

                Directory.CreateDirectory(path);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return path;
        }


        public string[] GetFiles(string dir, bool isRecursion)
        {
            if (isRecursion)
            {
                return FileHelper.GetSubFiles(dir);
            }
            else
            {
                return Directory.GetFiles(dir);
            }
        }

        #endregion


    }
}
